//
//  ZXRefreshHeader.m
//  imtMall
//
//  Created by Paddy-long on 15/8/25.
//  Copyright (c) 2015年 IMT. All rights reserved.
//

#import "ZXRefreshHeader.h"
#import "UIView+Positioning.h"


@implementation ZXRefreshHeader
{
    MJRefreshState lastState_;
    BOOL isAnimationing_;
    CGFloat gifView_right_X_;
}

- (void)prepare
{
    [super prepare];
    
//    self.lastUpdatedTimeLabel.hidden = YES;
//    [self setGifImages];
}

//-(void)placeSubviews
//{
//    [super placeSubviews];
//    [self resetGifViewFrame];
//}

- (void)setGifImages
{
    NSArray *array = @[@"",@""];
    
    [self setImages:array duration:array.count * 0.03 forState:MJRefreshStateIdle];
    [self setImages:array duration:array.count * 0.03 forState:MJRefreshStatePulling];
    [self setImages:array duration:array.count * 0.03 forState:MJRefreshStateRefreshing];
}

/**
 *  @author Paddy.long, 2016-02-02 10:51:48
 *
 *  重新调整gifView的位置
 */
-(void)resetGifViewFrame
{
    self.gifView.contentMode = UIViewContentModeScaleAspectFit;
    
    CGFloat height_ = 25;
    NSArray *array = @[@"",@""];
    UIImage *image = [array firstObject];
    CGFloat sacle = (image.size.width / image.size.height);
    self.gifView.size = CGSizeMake(height_ * sacle, height_);

    CGFloat arrowCenterX = self.mj_w * 0.5;
    CGFloat arrowCenterY = self.mj_h * 0.5;
    self.gifView.center = CGPointMake(arrowCenterX, arrowCenterY);
    if (self.stateLabel.hidden && self.lastUpdatedTimeLabel.hidden) {
    }
    else {
        self.gifView.right = arrowCenterX - 80;
    }
    gifView_right_X_ = self.gifView.right;
}


- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
    
    [self setGifAnimationWithSate:self.state];
}

- (void)setGifAnimationWithSate:(MJRefreshState)state
{    
    if (isAnimationing_) {
        return;
    }
    if (MJRefreshStateRefreshing == lastState_ && MJRefreshStateIdle == state) {
        isAnimationing_ = YES;
        [UIView animateWithDuration:0.25 animations:^{
            self.gifView.right = self.width;
        }completion:^(BOOL finished) {
            isAnimationing_ = NO;
            self.gifView.right = gifView_right_X_;
        }];
    }
    lastState_ = state;
}

@end



@implementation ZXRefreshAutoFooter

- (void)prepare
{
    [super prepare];
    
//    NSArray *images = @[@"",@""];
//    [self setImages:images duration:images.count * 0.03 forState:MJRefreshStateRefreshing];
}

//-(void)placeSubviews
//{
//    [super placeSubviews];
//    [self resetGifViewFrame];
//}

- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
    
    if (!self.isPreloading) {
        return;
    }
    
//    if ([WF_SWITCH_VIEW_MANAGER is3G]) {
//        return;
//    }
    
    if (self.state == MJRefreshStateRefreshing || self.hidden) {
        return;
    }
    
    
    UIScrollView *supV = (UIScrollView *)self.superview;
    if (supV && [supV isKindOfClass:[UIScrollView class]])
    {
        CGFloat old_offset_y = [change[@"old"] CGPointValue].y;
        CGFloat new_offset_y = [change[@"new"] CGPointValue].y;
        CGFloat diff_offset_y = new_offset_y - old_offset_y;
        
        CGFloat really_off =  (supV.contentSize.height - supV.height) - new_offset_y;//真正偏移量
        
        CGFloat threshold_off = supV.height;
//        WBCLogDebug(@"diff_offset_y:%@",@(diff_offset_y));
        if (
            diff_offset_y > 15 &&
            new_offset_y > 0)//new_y 大于零 为了排除下拉刷新时，松开自动回位，产生的快速下拉的影响
        {
            float rate = 2 + (2 * diff_offset_y / supV.height);//响应曲线
            rate = rate * rate;
            threshold_off = supV.height * rate;
//            WBCLogDebug(@"\noffsetY/diffY:[%@ / %@] \nthreshold/underOffH:[%@ / %@]\nrate/rateFixed[%0.2f / %0.2f]\n\n",
//                        @(new_offset_y),
//                        @(diff_offset_y),
//                        @(threshold_off),
//                        @(really_off),
//                        (2 * diff_offset_y / supV.height),
//                        rate);
        }
        
        BOOL scroll2Bottom = diff_offset_y > 0;
        
        if (scroll2Bottom &&
            really_off < threshold_off)
        {
            [self beginRefreshing];
        }
    }
}


/**
 *  @author Paddy.long, 2016-01-11 15:01:48
 *
 *  重新调整gifView的位置
 */
-(void)resetGifViewFrame
{
    self.gifView.contentMode = UIViewContentModeScaleAspectFit;
    
    CGFloat height_ = 35;
    NSArray *array = @[@"",@""];
    UIImage *image = [array firstObject];
    CGFloat sacle = (image.size.width / image.size.height);
    self.gifView.size = CGSizeMake(height_ * sacle, height_);
    
    CGFloat arrowCenterX = self.mj_w * 0.5;
    CGFloat arrowCenterY = self.mj_h * 0.5;
    self.gifView.center = CGPointMake(arrowCenterX, arrowCenterY);

    if (self.isRefreshingTitleHidden) {
        self.gifView.contentMode = UIViewContentModeCenter;
    } else {
        self.gifView.contentMode = UIViewContentModeRight;
        self.gifView.right = self.mj_w * 0.5 - 50;
    }
}

@end


